home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / TDOOR201.ZIP;1 / TESTDOOR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-04  |  2.3 KB  |  106 lines

  1. /*
  2.     testdoor.c - A sample door to demonstrate TriDoor
  3.     Copyright (c) 1992-1994 By Mark Goodwin
  4.  
  5.     Compile as follows:
  6.  
  7.     Borland C++    : bcc -ml testdoor.c bctdoor.lib
  8.     Microsoft C++  : cl /AL testdoor.c mctdoor.lib
  9.     Symantec C++   : sc -ml testdoor.c mctdoor.lib
  10.     Turbo C++      : tcc -ml testdoor.c tctdoor.lib
  11.     Watcom C++     : wcl -ml testdoor.c wctdoor.lib
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <time.h>
  18. #include "tridoor.h"
  19.  
  20. #define TRUE 1
  21. #define FALSE 0
  22.  
  23. void displaywelcome(void);
  24. void playgame(void);
  25.  
  26. #if defined( __WATCOMC__)  || defined(_MSC_VER)
  27. void randomize(void)
  28. {
  29.     srand(1);
  30. }
  31.  
  32. int random(int n)
  33. {
  34.     return rand() % (n - 1) + 1;
  35. }
  36. #endif
  37.  
  38. void main(int argc, char *argv[])
  39. {
  40.     TDInitialize(argc, argv);
  41.     strcpy(TDDoorName, "TestDoor 1.01");
  42.     randomize();
  43.     displaywelcome();
  44.     while (TRUE)
  45.         playgame();
  46. }
  47.  
  48. void displaywelcome(void)
  49. {
  50.     TDSetColor(WHITE, BLACK);
  51.     TDClrScr();
  52.     TDPrintf("TestDoor 1.01\n");
  53.     TDSetColor(YELLOW, BLACK);
  54.     TDPrintf("Copyright (c) 1992-1994 By Mark Goodwin\n\n");
  55. }
  56.  
  57. void playgame(void)
  58. {
  59.     int computernum, usernum, win, numguesses, input;
  60.     char line[81];
  61.  
  62.     TDSetColor(LIGHTCYAN, BLACK);
  63.     computernum = random(1000 + 1);
  64.     win = FALSE;
  65.     numguesses = 0;
  66.     TDSetColor(LIGHTMAGENTA, BLACK);
  67.     TDPrintf("The computer is thinking of a number from 1-1000!\n");
  68.     do {
  69.         numguesses++;
  70.         TDSetColor(LIGHTGREEN, BLACK);
  71.         TDPrintf("Enter a number from 1-1000 (0 to quit): ");
  72.         TDSetColor(LIGHTRED, BLACK);
  73.         TDGets(line, 81);
  74.         usernum = atoi(line);
  75.         if (usernum < 0 || usernum > 1000) {
  76.             TDSetColor(YELLOW, BLACK);
  77.             TDPrintf("Number must be between 1 and 1000!!\n");
  78.         }
  79.         if (usernum) {
  80.             if (computernum == usernum)
  81.                 win = TRUE;
  82.             else {
  83.                 TDSetColor(YELLOW, BLACK);
  84.                 if (usernum > computernum)
  85.                     TDPrintf("   Too high!\n");
  86.                 else
  87.                     TDPrintf("   Too low!\n");
  88.             }
  89.         }
  90.     } while (!win && usernum);
  91.     TDSetColor(YELLOW, BLACK);
  92.     if (win)
  93.         TDPrintf("You got it in %d guesses\n", numguesses);
  94.     TDPrintf("Play again (y/n)? ");
  95.     do {
  96.         input = TDGetch();
  97.         input = toupper(input);
  98.     } while (input != 'Y' && input != 'N');
  99.     TDSetColor(LIGHTCYAN, BLACK);
  100.     if (input =='N') {
  101.         TDPrintf("No\n\n");
  102.         exit(0);
  103.     }
  104.     TDPrintf("Yes\n\n\n");
  105. }
  106.